﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Sigavat.Entities;
using Sigavat.Service;

namespace Sigavat.Web.Controllers
{
    public class PromotorController : Controller
    {

        [Dependency]
        public ICentroComercialService centroSerivice { get; set; }
        [Dependency]
        public IPromotorService promotorService { get; set; }
        //
        // GET: /Promotor/

        public ActionResult Index()
        {
            var promotor = promotorService.GetFromPromotoresByCriterio("");
            return View(promotor);
        }

        [HttpPost]
        public ActionResult Index(string criterio)
        {
            var promotor = promotorService.GetFromPromotoresByCriterio("");
            return View(promotor);
        }
        public ActionResult Details(Int32 id)
        {
            var promotor = promotorService.GetFromPromotorBy(id);

            return View("Details", promotor);
        }

        public ActionResult Create()
        {
            
            var centroC = centroSerivice.GetAllFromCentroComerciales();
            ViewData["Id"] = new SelectList(centroC, "Id", "NombreCentroC");

            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(Promotor promotor)
        {
            var ok = false;
            var msg = "Error";

            if (ModelState.IsValid)
            {
                promotorService.AddPromotor(promotor);
                ok = true;
                msg = "Promotor Guardado correctamente";
                return RedirectToAction("Index");
            }

            var rpt = new
            {
                ok = ok,
                msg = msg
            };
            return Json(rpt, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(Int32 id)
        {
            promotorService.RemovePromotor(id);

            var promotor = promotorService.GetFromPromotoresByCriterio("");
            return View("Index",promotor);
        }

        public ActionResult Edit(Int32 id)
        {
            var promotor = promotorService.GetFromPromotorBy(id);
            
            var ccomercial = centroSerivice.GetAllFromCentroComerciales();

            
            ViewData["IdCentroComercial"] = new SelectList(ccomercial, "Id", "nombreCentroComercia", promotor.IdCentroComercial);

            return View("Edit", promotor);
        }
        [HttpPost]
        public ActionResult Edit(Promotor promotor)
        {
            var ok = false;
            var msg = "ERROR";

            if (ModelState.IsValid)
            {
                promotorService.UpdatePromotor(promotor);
                ok = true;
                msg = "Promotor Actualizado correctamente";
            }

            var rpt = new
            {
                ok = ok,
                msg = msg
            };

            return Json(rpt, JsonRequestBehavior.AllowGet);
        }

    }
}
